"use strict";

// 全局css样式
import "./css/common.scss";
import "./css/style/common.scss";
import "./css/style/default.scss";
import "./css/style/vip.scss";
import "./css/style/light.scss";
import "./css/style/iview.scss";
import './font/iconfont.css'
import "./css/menu.scss";
import "./css/table.scss";
import "./css/dialog.scss";
import "./css/map.scss";


// 组件引用
import Tab from "./view/components/Tab.vue"
import Menu from "./view/components/Menu.vue"
import Table from "./view/components/plug/Table.vue"
import pageTemp from "./view/components/pageTemp.vue"

// 混入
import commonMixin from './js/commonMixin'
import routeMixin from './js/routeMixin'
import apiMixin from './js/apiMixin'
import powerMixin from './js/powerMixin'

// 状态管理
import vbStore from './store/store'
// api接口
import vbApi from './axios/api'
// 路由
import vbRouter from './router/router'

// 共通方法
import util from "./js/util";
import apiUtil from "./js/apiUtil";

/**
 * 安装方法类和插件
 *
 * @param app
 */
const install = (app, options) => {
    app.component('vb-tab', Tab)
    app.component('vb-menu', Menu)
    app.component('vb-table', Table)
    app.component('vb-page', pageTemp)

    app.mixin(commonMixin)
    app.mixin(routeMixin)
    app.mixin(apiMixin)
    app.mixin(powerMixin)

    // 状态管理
    let store = null
    if (options.store) {
        store = options.store
    } else {
        let storeClass = new vbStore()
        store = storeClass.init()
    }
    app.use(store)

    // 路由
    let router = null
    if (options.router) {
        router = options.router
    } else {
        let routerClass = new vbRouter({ routes: [], store: store })
        router = routerClass.init()
    }
    app.use(router)

    // 加载api接口
    let api = new vbApi(router, {
        response: options.axios?options.axios.response:null,
        request: options.axios?options.axios.request:null,
    })

    // 加载方法类
    app.config.globalProperties.$vbUtils = util;
    app.config.globalProperties.$apiUtils = apiUtil;
    app.config.globalProperties.$api = api;

    // 初始化缓存系统数据
    let apiData = sessionStorage.getItem("_apiData")
    let menuData = sessionStorage.getItem("_menuData")
    let routerData = sessionStorage.getItem("_routerData")
    let systemConfig = sessionStorage.getItem("_systemConfig")
    if (apiData) { store.commit("setApiData", JSON.parse(apiData)); }
    if (menuData) { store.commit("setMenuData", JSON.parse(menuData)); }
    if (routerData) { store.commit("setRouterData", JSON.parse(routerData)); }
    if (systemConfig) { store.commit("setSystemConfig", JSON.parse(systemConfig)); }

    // 加载系统配置数据
    api.getSystemConfig().then(res => {
        store.commit("setSystemConfig", res.config);
        if (res.defaultRoute) {
            store.commit("setDefaultRoutePath", res.defaultRoute.path);
        }
    })
    // 加载初始化系统数据
    if (sessionStorage.token) {
        api.getInitData().then(res => {
            routeMixin.methods.initRouterUrl(res.routerList, router)
            store.commit("setMenuData", res.menuList);
            store.commit("setApiData", res.apiList);
            store.commit("setRouterData", res.routerList);
        })
    }
};

export default {
    install,
};
